package org.vectrics.recruiter.web.tags;

import java.text.SimpleDateFormat;
import java.util.Iterator;

import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Category;
import org.vectrics.VectricsException;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Combo;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.history.Activity;
import org.vectrics.history.HistoryProvider;
import org.vectrics.location.Location;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobFunction;

public class ExperienceEditTag extends TagSupport {

    private static final long serialVersionUID = 835540061987579235L;

    static private Category log = Category.getInstance(ExperienceEditTag.class);

    private static SimpleDateFormat historyDateFormat = new SimpleDateFormat("dd/MM/yyyy");

    public ExperienceEditTag() {
    }

    Combo makeFunctionCombo(final Job job) throws VectricsException {
        Combo combo = new Combo();
        combo.setStyle("width:250;font-family:verdana,arial,sans serif;font-size=11px");
        Iterator iter = RecruitServiceLocator.getJobProvider().findAllJobFunctions().iterator();

        while (iter.hasNext()) {
            JobFunction function = (JobFunction) iter.next();
            combo.addOption(function.getDescription(), function.getCode());
        }
        return combo;
    }

    Combo makeLocationCombo(final Job job) throws Exception {
        Combo combo = new Combo();
        combo.setStyle("width:250;font-family:verdana,arial,sans serif;font-size=11px");
        Iterator iter = CoreServiceLocator.getLocationService().findAllLocations(job.getCompany().getDomain())
                .iterator();

        while (iter.hasNext()) {
            Location location = (Location) iter.next();
            combo.addOption(location.getName(), location.getId().toString());
        }
        return combo;
    }

    protected Table makeHistoryTable(final Job job) {
        log.debug("makeHistoryTable() called.");
        BorderTable table = new BorderTable();
        table.setWidth(500);

        Row row = table.addRow();
        Cell cell = row.addCell();
        cell.addElement(TextHelper.getFont("subFont", "<b>Date</b>"));
        cell = row.addCell();
        cell.addElement(TextHelper.getFont("subFont", "<b>User</b>"));
        cell = row.addCell();
        cell.addElement(TextHelper.getFont("subFont", "<b>Action</b>"));
        cell = row.addCell();
        cell.addElement(TextHelper.getFont("subFont", "<b>Description</b>"));

        try {
            Iterator iter = HistoryProvider.getInstance()
                    .findObjectHistoryEntries(Job.OBJECT_TYPE, job.getId().toString()).iterator();
            while (iter.hasNext()) {
                Activity history = (Activity) iter.next();
                row = table.addRow();
                cell = row.addCell();
                cell.addElement(TextHelper.getFont("subFont", historyDateFormat.format(history.getDate())));
                cell = row.addCell();
                cell.addElement(TextHelper.getFont("subFont", history.getPerson().getName()));
                cell = row.addCell();
                cell.addElement(TextHelper.getFont("subFont", history.getActivityType().getDescription()));
                cell = row.addCell();
                cell.addElement(TextHelper.getFont("subFont", history.getDescription()));
            }

        } catch (Exception ex) {
            log.error("makeHistoryTable() - exception", ex);
        }

        return table;
    }

}
